<?php

namespace Shop\AdminBundle\Controller;

use Shop\AdminBundle\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Shop\AdminBundle\Controller\BaseController;

use Shop\AdminBundle\Entity\Brand;
use Shop\AdminBundle\Form\BrandType;

use Symfony\Component\HttpFoundation\Session;

/**
 * @Route("/admin/brand")
 */
class BrandController extends BaseController
{
    /**
     * 品牌列表页
     *
     * @Route("/", name="admin_brand")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
//        $xbb=$this->get('xiongbaba');

        $repo=$this->getRepo('brand');
        return $this->paginate($repo);
    }

    /**
     * 添加品牌，表单处理
     *
     * @Route("/", name="admin_brand_create")
     * @Method("POST")
     * @Template("ShopAdminBundle:Brand:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new Brand();
        $form = $this->createForm(new BrandType(), $entity, array(
            'action' => $this->generateUrl('admin_brand_create'),
            'method' => 'POST',
        ));

        $form->handleRequest($request);

        if ($form->isValid()) {
            //处理logo图片
            $logoFile=$entity->getLogo();
            $fileName=$this->handleUpload($logoFile);
            $entity->setLogo($fileName);

            //description去除html标签
//            $entity->setDescription(html_entity_decode($entity->getDescription()) );

            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();
            return $this->redirect($this->generateUrl('admin_brand'));
        }

        return $this->redirect($this->generateUrl('admin_brand_new'));
    }

    /**
     * 添加品牌页面
     *
     * @Route("/new", name="admin_brand_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Brand();
        $form = $this->createForm(new BrandType(), $entity, array(
            'action' => $this->generateUrl('admin_brand_create'),
            'method' => 'POST',
        ));

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'has_upload'=>true,
            'ajax_form'=>true
        );
    }

    /**
     * 更新品牌页面
     *
     * @Route("/{id}/edit", name="admin_brand_edit")
     * @Method("GET")
     * @Template("ShopAdminBundle:Brand:new.html.twig")
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ShopAdminBundle:Brand')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Brand entity.');
        }

        $form = $this->createForm(new BrandType(), $entity, array(
            'action' => $this->generateUrl('admin_brand_update', array('id' => $entity->getId())),
            'method' => 'POST',
        ));

        return array(
            'entity'      => $entity,
            'form'   => $form->createView(),
            'is_edit'=> true
        );
    }

    /**
     * 更新品牌，表单处理
     *
     * @Route("/{id}", name="admin_brand_update")
     * @Method("POST")
     * @Template("ShopAdminBundle:Brand:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ShopAdminBundle:Brand')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Brand entity.');
        }

        $editForm = $this->createForm(new BrandType(), $entity, array(
            'action' => $this->generateUrl('admin_brand_update', array('id' => $entity->getId())),
            'method' => 'POST',
        ));

        //pre save logo path
        $formName=$editForm->getName();
        $fileBag=$request->files->get($formName);

        if(!isset($fileBag['logo'])){
            $originFileName=$entity->getLogo();
        }

        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            if(isset($originFileName)){
                $entity->setLogo($originFileName);
            }
            else{
                $picFile=$entity->getLogo();
                $fileName= $this->handleUpload($picFile);
                $entity->setLogo($fileName);
            }

            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);

            $em->flush();
            return $this->redirect($this->generateUrl('admin_brand'));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView()
        );

    }

    /**
     * 删除品牌
     *
     * @Route("/{id}/delete", name="admin_brand_delete")
     * @Method("GET")
     */
    public function deleteAction(Request $request, $id)
    {
        return $this->deleteRow('brand',$request, $id);
    }

}
